package com.ccjq.jq173.shosestore.core.db.manager;

import java.sql.*;

public class DbConnection {
    private static final String DRIVER;
    private static final String URL;
    private static final String USERNAME;
    private static final String PASSWORD;

    static {
        DbConfigReader dbConfigReader = new DbConfigReader();
        DRIVER = dbConfigReader.getValue("db.driver");
        URL = dbConfigReader.getValue("db.url");
        USERNAME = dbConfigReader.getValue("db.username");
        PASSWORD = dbConfigReader.getValue("db.password");
    }

    /*
    建立数据库连接
     */
    public static Connection getConnection() {
        Connection connection = null;

        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;
    }

    /*
    关闭数据库相关资源
     */

    public static void close(ResultSet rs, Statement st, Connection conn)
    {
        try
        {
            if(rs!=null)
                rs.close();
            if(st!=null)
                st.close();
            if(conn!=null)
                conn.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(ResultSet rs)
    {
        try
        {
            if(rs!=null)
                rs.close();

        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(Statement st)
    {
        try
        {
            if(st!=null)
                st.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(Connection conn)
    {
        try
        {
            if(conn!=null)
                conn.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String args[] )
    {
        System.out.println(DbConnection.getConnection());
    }
}
